package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Function;
import com.xx.pmis.services.FunctionService;
import com.xx.pmis.web.forms.FunctionForm;

/**
 * @class 菜单Action类 <br>
 * 
 * @editor 石星星  @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class FunctionAction extends PMISBaseAction  {
	private static final Log log = LogFactory.getLog(FunctionAction.class);
	FunctionService functionService = getPMISServices().getFunctionService();
	
	// 查询所有菜单
	public ActionForward listFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = functionService.getAllParentFunction();
		} catch (Exception e) {
			log.info("查询所有菜单异常" + e.toString());
		}
		request.setAttribute("position", "菜单管理/菜单");
		request.setAttribute("functionsList", all);
		return mapping.findForward("listFunction");
	}
	
	// 查询所有二级菜单
	public ActionForward listNextFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String functionParentID = request.getParameter("functionParentID");
		log.info("******************************************************************2->" + functionParentID);
		
		List<Function> all = new ArrayList<Function>();
		Function function = null;
		try {
			function = functionService.getFunctionByFunctionID(Integer.parseInt(functionParentID));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		all.add(function);
		try {
			List<Function> list = functionService.getAllFunctionByFunctionParentID(Integer.parseInt(functionParentID));
			for(Function f : list) {
				all.add(f);
			}
		} catch (Exception e) {
			log.info("根据父ID查询二级菜单异常" + e.toString());
		}


		request.setAttribute("position", "菜单管理/二级菜单");
		request.setAttribute("functionNextList", all);
		return mapping.findForward("listNextFunction");
	}
	
	// 新增
	public ActionForward addFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// request.setAttribute("FunctionsDate", CurrentDate.currentDate());
		String functionID = request.getParameter("functionID");
		request.setAttribute("functionID", functionID);
		request.setAttribute("position", "菜单/新增");
		return mapping.findForward("addFunction");
	}
	
	// 新增，处理方法
	public ActionForward addFunction_Do(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		FunctionForm f = (FunctionForm) form;

		int functionID = f.getFunctionID();// ID
		int functionParentID = f.getFunctionParentID();// 父ID
		String functioName = f.getFunctioName();// 菜单名称
		String functionDescription = f.getFunctionDescription();// 描述
		String functionURL = f.getFunctionURL();// URL
		String functionTilte= f.getFunctionTilte();// 菜单标题
		String functionTarget = f.getFunctionTarget();// TARGET
		String functionIcon = f.getFunctionIcon();// ICON
		String functionIconOpen = f.getFunctionIconOpen();// ICONOPEN
		String functionOpened = f.getFunctionOpened();// OPENED

		Function function = new Function();
		function.setFunctionID(functionID);
		function.setFunctionParentID(functionParentID);
		function.setFunctioName(functioName);
		function.setFunctionDescription(functionDescription);
		function.setFunctionURL(functionURL);
		function.setFunctionTilte(functionTilte);
		function.setFunctionTarget(functionTarget);
		function.setFunctionIcon(functionIcon);
		function.setFunctionIconOpen(functionIconOpen);
		function.setFunctionOpened(functionOpened);
		function.setFunctionState("1");
	
		try {
			functionService.saveOrUpdateFunction(function);
		} catch (Exception e) {
			log.info("保存菜单异常！" + e.toString());
		}
	
		if(functionParentID == -1 || functionParentID == 1) {
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		} else {
			request.setAttribute("functionParentID", functionParentID);
			this.listNextFunction(mapping, form, request, response);
			return mapping.findForward("listNextFunction");
		}
	}
	
	// 编辑
	public ActionForward editFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String functionID = request.getParameter("functionID");
		Function function = new Function();
		try {
			function = functionService.getFunctionByFunctionID(Integer.parseInt(functionID));
		} catch (Exception e) {
			log.info("根据菜单ID查询该菜单异常！" + e.toString());
		}

		request.setAttribute("position", "菜单/编辑");
		request.setAttribute("function", function);
		return mapping.findForward("editFunction");
	}

	// 编辑，处理方法
	public ActionForward editFunction_Do(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		FunctionForm f = (FunctionForm) form;
		int functionID = f.getFunctionID();// ID
		int functionParentID = f.getFunctionParentID();// 父ID
		String functioName = f.getFunctioName();// 菜单名称
		String functionDescription = f.getFunctionDescription();// 描述
		String functionURL = f.getFunctionURL();// URL
		String functionTilte= f.getFunctionTilte();// 菜单标题
		String functionTarget = f.getFunctionTarget();// TARGET
		String functionIcon = f.getFunctionIcon();// ICON
		String functionIconOpen = f.getFunctionIconOpen();// ICONOPEN
		String functionOpened = f.getFunctionOpened();// OPENED

		Function function = null;
		try {
			function = functionService.getFunctionByFunctionID(functionID);
		} catch (Exception e) {
			log.info("根据菜单ID查询该菜单异常！" + e.toString());
		}
		function.setFunctionID(functionID);
		function.setFunctionParentID(functionParentID);
		function.setFunctioName(functioName);
		function.setFunctionDescription(functionDescription);
		function.setFunctionURL(functionURL);
		function.setFunctionTilte(functionTilte);
		function.setFunctionTarget(functionTarget);
		function.setFunctionIcon(functionIcon);
		function.setFunctionIconOpen(functionIconOpen);
		function.setFunctionOpened(functionOpened);
		
		if(functionParentID == -1 || functionParentID == 1) {
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		} else {
			request.setAttribute("functionParentID", functionParentID);
			this.listNextFunction(mapping, form, request, response);
			return mapping.findForward("listNextFunction");
		}
	}

	// 删除
	public ActionForward delFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Function function = null;
		FunctionForm f = (FunctionForm) form;
		String[] ids = f.getIds();
		
		int functionParentID = 1;
		for (int i = 0; i < ids.length; i++) {
			try {
				function = functionService.getFunctionByFunctionID(Integer.parseInt(ids[i]));
				functionParentID = function.getFunctionParentID();
				log.info("******************************************************************1->" + functionParentID);
			} catch (Exception e) {
				log.info("根据菜单ID查询该菜单异常！" + e.toString());
			}
			try {
				functionService.deleteFunction(function);
			} catch (Exception e) {
				log.info("保存菜单异常！" + e.toString());
			}
		}
		
		if(functionParentID == -1 || functionParentID == 1) {
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		} else {
			request.setAttribute("functionParentID", functionParentID);
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		}
	}
	
    // 停用菜单
	public ActionForward stopFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String functionID = request.getParameter("functionID");

		Function function = null;
		int functionParentID = 1;
		try {
			function = functionService.getFunctionByFunctionID(Integer.parseInt(functionID));
			functionParentID = function.getFunctionParentID();
			log.info("******************************************************************1->" + functionParentID);
		} catch (Exception e) {
			log.info("根据菜单ID查询该菜单异常！" + e.toString());
		}
		function.setFunctionState("1");

		try {
			functionService.saveOrUpdateFunction(function);
		} catch (Exception e) {
			log.info("保存菜单异常！" + e.toString());
		}
		
		if(functionParentID == -1 || functionParentID == 1) {
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		} else {
			request.setAttribute("functionParentID", functionParentID);
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		}
	}
	
    // 启用菜单
	public ActionForward startFunction(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String functionID = request.getParameter("functionID");

		Function function = null;
		int functionParentID = 1;
		try {
			function = functionService.getFunctionByFunctionID(Integer.parseInt(functionID));
			functionParentID = function.getFunctionParentID();
			log.info("******************************************************************1->" + functionParentID);
		} catch (Exception e) {
			log.info("根据菜单ID查询该菜单异常！" + e.toString());
		}
		function.setFunctionState("0");

		try {
			functionService.saveOrUpdateFunction(function);
		} catch (Exception e) {
			log.info("保存菜单异常！" + e.toString());
		}

		if(functionParentID == -1 || functionParentID == 1) {
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		} else {
			request.setAttribute("functionParentID", functionParentID);
			this.listFunction(mapping, form, request, response);
			return mapping.findForward("listFunction");
		}

	}

}  